package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.service.YddUserService;
import com.itheima.youdianda.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HomeController
 * @Description 前台登录
 * @Author Young
 * @Date 2023/2/14 19:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private YddUserService yddUserService;

    /**
     * token中返回 手机号 用户id
     * @param yddUser
     * @return
     */
    @PostMapping("/index/login")
    public R<Map> login(@RequestBody YddUser yddUser){
        System.out.println("这里运行了");
        //获取密码
        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex((password+"abc").getBytes());
        //用手机号查询数据库
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getMobile,yddUser.getMobile());
        YddUser user = yddUserService.getOne(queryWrapper);
        //判断用户是否存在
        if (user == null){
            return R.error("登录失败");
        }
        if (!(user.getPassword().equals(password))){
            return R.error("密码错误");
        }
        //登录成功,颁发token
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("userinfo",user);
        Map map = JwtUtils.giveToken(tokenMap);

        // 组装token返回给前端
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("id", user.getId());
        userMap.put("mobile", user.getMobile());
        Map resultMap = JwtUtils.giveToken(userMap);

        return R.success(resultMap);
    }


    /**
     * token返回详细的信息, 封装在userinfo对象中
     * @param yddUser
     * @return
     */
    //@PostMapping("/index/login")
    public R<Map> login2(@RequestBody YddUser yddUser){
        System.out.println("这里运行了");
        //获取密码
        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //用手机号查询数据库
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getMobile,yddUser.getMobile());
        YddUser user = yddUserService.getOne(queryWrapper);
        //判断用户是否存在
        if (user == null){
            return R.error("登录失败");
        }
        if (!(user.getPassword().equals(password))){
            return R.error("密码错误");
        }
        //登录成功,办法token
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("userinfo",user);
        Map map = JwtUtils.giveToken(tokenMap);

        //封装数据准备返回
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("data",map);
        System.out.println(map);
        return R.success(map);
    }
}
